package com.sesame.user.service;

import com.codingapi.txlcn.tc.annotation.TxcTransaction;
import com.sesame.feign.FeignServiceTx2;
import com.sesame.feign.FeignServiceTx3;
import com.sesame.user.bean.User;
import com.sesame.user.dao.UserDao;
import kim.sesame.framework.exception.BusinessException;
import kim.sesame.common.result.ApiResult;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * UserService
 *
 * @author admin
 * @date 2019-01-30 13:03:18
 * @Description:
 */
@CommonsLog
@Service
public class UserService {

    @SuppressWarnings("all")
    @Resource
    private UserDao userDao;

    @SuppressWarnings("all")
    @Autowired
    private FeignServiceTx2 feignServiceTx2;

    @SuppressWarnings("all")
    @Autowired
    private FeignServiceTx3 feignServiceTx3;

    /**
     * 查询list
     *
     * @author admin
     * @date 2019-01-30 13:03:18
     * @Description: 分页时要注意
     */
    public List<User> searchList() {

//		List<User> list = userDao.searchList(bean);

        List<User> list = feignServiceTx2.getList().getResult();

        return list;
    }


    /**
     * 修改
     *
     * @author admin
     * @date 2019-01-30 13:03:18
     * @Description:
     */
    @TxcTransaction
    @Transactional(rollbackFor = Exception.class)
    public int update(User bean) {
        bean.setId("1");
        int res = userDao.update(bean);

        Response result2 = feignServiceTx2.save("2", bean.getUpdateUser());
        Response result3 = feignServiceTx3.save("3", bean.getUpdateUser());

        if (result2.isSuccess() && result3.isSuccess()) {

        } else {
            throw new BusinessException("交互异常");
        }

        if (bean.getUpdateUser().equals("001")) {
            throw new BusinessException("模拟异常");
        }

        return res;
    }

}
